﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace FACUI.BasicClass
{
    public class KglCollection : IEnumerable
    {
        private Dictionary<int, string> kglDict = new Dictionary<int, string>();
        private Dictionary<int, int> kglIndex = new Dictionary<int, int>();
        private List<Kgl> kglList = new List<Kgl>();

        public bool Add(Kgl kgl)
        {
            if (kgl.KglName == null)
            {
                return false;
            }
            try
            {
                this.kglList.Add(kgl);
                if (!this.kglIndex.ContainsKey(kgl.id))
                {
                    this.kglIndex.Add(kgl.id, this.kglList.Count - 1);
                }
                if (!this.kglDict.ContainsKey(kgl.id))
                {
                    this.kglDict.Add(kgl.id, kgl.KglName);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            return true;
        }

        public void Clear()
        {
            try
            {
                this.kglDict.Clear();
                this.kglList.Clear();
                this.kglIndex.Clear();
            }
            catch
            {
            }
        }

        public bool Contains(string kglName)
        {
            if (this.kglDict == null)
            {
                return false;
            }
            return this.kglDict.ContainsValue(kglName);
        }

        public bool Remove(Kgl kgl)
        {
            if ((kgl != null) && this.kglIndex.ContainsKey(kgl.id))
            {
                this.kglList.RemoveAt(this.kglIndex[kgl.id]);
                this.kglIndex.Clear();
                int num = 0;
                foreach (Kgl kgl2 in this.kglList)
                {
                    this.kglIndex.Add(kgl2.id, num);
                    num++;
                }
                if (this.kglDict.ContainsKey(kgl.id))
                {
                    this.kglDict.Remove(kgl.id);
                }
                return true;
            }
            return false;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.kglList.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return this.kglDict.Keys.Count;
            }
        }

        public Kgl this[int index]
        {
            get
            {
                return this.kglList[index];
            }
        }
    }
}

